<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>添加权限</title>
<link rel="stylesheet" href="/scriptZIP/layui.min.css">
	<link rel="stylesheet" href="../css/check_box.css">
	<link rel="stylesheet" href="../css/radio.css">
	<style>
		.checkbox-inline {
			width: calc(100% / 4.5);
		}

		.i-checks input:checked+i:before {
			top: 1px;
			left: 1px;
			width: 8px;
			height: 8px;
		}

		.i-checks>i {
			width: 12px;
			height: 12px;
		}

		.offer {
			width: 130px;
			height: 40px;
			background: #0086b6;
			color: white;
			text-align: center;
			line-height: 40px;
			font-size: 16px;
		}

		.return {
			width: 130px;
			height: 40px;
			background: #0086b6;
			color: white;
			text-align: center;
			line-height: 40px;
			margin-top: -40px;
			margin-left: 180px;
			font-size: 16px;
		}

		.labels {
			width: 70%;
			min-width: 600px;
			margin-top: -20px;
			margin-left: 110px;
		}

		.label {
			margin-top: 0;
		}

		.layui-input-block.true {
			padding-top: 13px;
			line-height: 15px;
		}

		.layui-input-block.true>input:not(:first-child) {
			margin-left: 20px;
		}

		[v-show],
		[v-if] {
			display: none;
		}
	</style>
</head>

<body>

	<div class="layui-row" id="vue-root">
		<div></div>
		<div class="layui-col-md6 layui-col-md-offset3">
			<div class="layui-form-item">
				<label class="layui-form-label">权限组名称</label>
				<div class="layui-input-block">
					<input name="deviceName" autocomplete="off" placeholder="请输入名称" class="layui-input"
						v-model="item1.name" style="width: 50%">
				</div>
			</div>

			<!-- <div class="layui-form-item">
				<label class="layui-form-label">密码</label>
				<div class="layui-input-block">
					<input name="deviceName" type="password" autocomplete="off" placeholder="请输入密码" class="layui-input"
						v-model="item1.password" style="width: 50%">
				</div>
			</div> -->

			<div v-for="(item,index) in list" :key="index" v-if="some1(item)">
				<div class="layui-form-item">
					<label class="layui-form-label" style="font-weight: bold;font-size: 16px">{{item.name}}</label>
					<div class="layui-input-block true">

						<label class="checkbox-inline i-checks" style="margin:0 0 0 20px">
							<input type="checkbox" style="display: none" v-model="item.fold">
							{{item.fold?'展开':'折叠'}}
						</label>
						<label class="checkbox-inline i-checks" style="margin:0 0 0 20px">
							<input type="checkbox" style="display: none" v-model="item.all" @change="doAll(item)">
							{{item.all?'全不选':'全选'}}
						</label>
					</div>
				</div>

				<div class="layui-form-item" style="margin-left: 60px" v-for="(items,index) in item.list" :key="index"
					v-if="!item.fold&&some(parent[items.key])">
					<label class="layui-form-label">{{items.name}}</label>

					<div class="labels">
						<label class="checkbox-inline i-checks" v-for="(a,index) in items.allows" :key="index"
							v-if="parent[items.key]&&parent[items.key][index]">
							<input type="checkbox" v-model="items.allows[index]">
							<i></i> {{['查看','新增','修改','删除'][index]}}
						</label>
					</div>
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<div class="offer" @click="submit">立即提交</div>
					<div class="return" onclick="history.back()">取消</div>
				</div>
			</div>
		</div>

	</div>

	<script src="/scriptZIP/qs.min.js"></script>
	<script src="/scriptZIP/vue.min.js"></script>
	<script src="/scriptZIP/axios.min.js"></script>

	<script src="/js/common.js"></script>
	<script src="/js/item.js"></script>
	<script src="/js/auth_util.js"></script>
	<script src="/js/menu.js"></script>


	<script>
		new Vue(merge({
			data: {
				item1: {
					name: '',
					// password: '',
				},
				list: list,
				parent: {},
				url: '/pc/auth'
			},
			created: function () {
				axios.get('/pc/auth/' + top_params().id).then(function (res) {
					var o = res.data.authority || {};
					for (var key in o) {
						o[key] = f(o[key]);
					}

					this.list.forEach(function (it1) {
						it1.list.forEach(function (it) {
							if (it.top)
								o[it.key] = o[it.key] ? and(f(it.top), o[it.key]) : f(it.top);
							else o[it.key] = [true, true, true, true];
						});
					});

					this.parent = o;
					this.init();
				}.bind(this));
			},
			
			methods: {
				doAll: function (o) {
					o.list.forEach(function (it) {
						it.allows = [o.all, o.all, o.all, o.all];
					});
				},
				some: function (it) {
					return it && it.some(function (i) {
						return i;
					});
				},
				some1: function (it) {
					return it.list.some(function (it1) {
						return this.some(this.parent[it1.key]);
					}.bind(this));
				},
				formatData: function (data) {
					var a = data.authority;
					delete data.authority;
					this.item1 = data;
					this.list.forEach(function (it1) {
						it1.list.forEach(function (it) {
							it.allows = f(a[it.key]);
						});
					});
				},
				submit: function () {
					var o = {};
					var p = this.parent;
					this.list.forEach(function (it1) {
						it1.list.forEach(function (it) {
							o[it.key] = g(and(it.allows, p[it.key]));
						});
					});
					this.item1.authority = o;
					this.item = this.item1;
					if (this.item.id) {
						axios.put('/pc/auth', this.item).then(function (res) {
							alert('成功');
							history.back();
						});
					} else {
						this.item.schoolId = top_params().id;
						axios.post('/pc/auth', this.item).then(function (res) {
							alert('添加成功');
							history.back();
						});
					}

				}
			}
		}));
	</script>


</body>

</html>